package com.hdax.dm.filter;

import cn.hutool.crypto.SecureUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hdax.dm.dto.user.TokenDto;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Component
public class AuthFilter extends ZuulFilter {

    @Resource
    private RedisTemplate<String,HashMap<String,String>> redisTemplate;

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        //如果请求路径带有permission，则需要鉴权
        RequestContext ctx = RequestContext.getCurrentContext();
        String requestURI = ctx.getRequest().getRequestURI();
        return requestURI.contains("/permission/");
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

        //是否有token
        String token = request.getHeader("token");
        if (token == null) {
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(403);
            ctx.setResponseBody("{\"errorCode\":\"10010\",\"msg\":\"操作失败,没有有效地用户信息\"}");
            ctx.getResponse().setContentType("application/json;charset=UTF-8");
            return null;
        }

        //是否有redisToken
        HashMap<String,String> redisToken = redisTemplate.opsForValue().get(token);
        if (token == null || redisToken == null) {
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(403);
            ctx.setResponseBody("{\"errorCode\":\"10010\",\"msg\":\"操作失败,没有有效地用户信息\"}");
            ctx.getResponse().setContentType("application/json;charset=UTF-8");
        }

        //是否即将过期
//        Long expireTime = redisTemplate.getExpire(token);
//        if(expireTime < 300){
//            //重新设置token的过期时间不行吗？
//            //ctx.set("token",buffer.toString());
//
//        }

        //将缓存时间重置为2小时
        redisTemplate.expire(token,2L,TimeUnit.HOURS);
        return null;
    }
}
